//
//  main.m
//  OC04_NSString
//
//  Created by dllo on 15/12/3.
//  Copyright © 2015年 dllo. All rights reserved.
//

#import <Foundation/Foundation.h>

int main(int argc, const char * argv[]) {
    @autoreleasepool {

    
# pragma mark - 知识点1 不可变字符串
    /* Class: NSString */
        
# pragma mark ** 创建对象相关API
#if 0
        /* 字面量方式,创建字符串对象,是常用方法 */
        NSString *str2 = @"iphone";
        
        NSString *str = [[NSString alloc]
                         initWithString:str2];
        NSLog(@"str2:%@", str2);
        NSLog(@"str:%@", str);
        
        /*带format参数方法,比较灵活(万能方法), 可以将任意内容以格式化方法生成字符串对象 */
        NSString *str3 = [[NSString alloc]initWithFormat:@"新建字符串,%@",str];
        NSLog(@"str3:%@",str3);
        
        int a = 1234522;
        NSString *str4 = [[NSString alloc]initWithFormat:@"%d",a];
        NSLog(@"str4: %@",str4);
        
        
        
        NSString *str5 = [NSString stringWithString:str4];
        NSLog(@"%@", str5);
        
        NSString *str6 = [NSString stringWithFormat:@"%@",str5];
        NSLog(@"%@", str6);

# pragma mark ** 直接获取字符串长度 API length
        /*长度*/
        NSString *str = @"iphone";
        NSLog(@"%ld", str.length);
# pragma mark ** 直接获取某一个字符 API character
        /* 取出h字符 */
        unichar c = [str characterAtIndex:2];
        NSLog(@"%c",c);
        
# pragma mark ** 直接判断两个字符串是否相等 API isEqual
       
        NSString *str1 = @"iphone";
        NSString *str2 = @"iPhone";
        
        if([str1 isEqualToString:str2]){
            
            NSLog(@"用户名正确");
        }else{
            NSLog(@"用户名错误");
        }
        

        
# pragma mark ** 字符串直接比较 API compare
        
        NSString *str1 = @"Zhangsan";
        NSString *str2 = @"Zhangxiaoming";
        
        [str1 compare:str2];
        NSLog(@"%ld",[str1 compare:str2]);

# pragma mark ** 直接获取一个字符串中某个部分 (子字符串) API substring range(范围)
        
        NSString *str = @"I have an iphone6s";
        /*取出I*/
        NSString *subStr = [str substringToIndex:1];
        NSLog(@"subStr:%@", subStr);
        /*取出iPhone6s*/
        NSString *subStr1 = [str substringFromIndex:10];
        NSLog(@"subStr1:%@", subStr1);
        
        /*取出have*/
        NSRange range = {2, 4};
        NSString *subStr2 = [str substringWithRange:range];
        NSLog(@"subStr2:%@",subStr2);
        /*取出an*/
        NSRange range2 = [str rangeOfString:@"an"];
        NSString *subStr3 = [str substringWithRange:range2];
        NSLog(@"subStr3:%@",subStr3);

        
# pragma mark ** 直接对字符串进行拼接 API stringByAppending Format(格式化)
        
        NSString *str1 = @"I have";
        NSString *str2 = @"an Pro";
        /*1*/
        NSString *str3 = [str1 stringByAppendingString:str2];
        NSLog(@"%@",str3);
        /*2*/
        NSLog(@"%@",[str1 stringByAppendingString:str2]);
        /*3*/
        NSString *str4 = [str1 stringByAppendingFormat:@" %@",str2];
        NSLog(@"str4:%@",str4);

# pragma mark ** 替换字符串 API stringByReplacing
        
        NSString *str = @"I love China";
        /* 需求  将China 换成 you */
        NSString *str2 = [ str stringByReplacingOccurrencesOfString:@"China" withString:@"you"];
        NSLog(@"%@",str2);
        

        
# pragma mark ** 字符串@"23413"直接转换为int类型 API intValue;
        
        NSString *str = @"12345";
        int a = str.intValue;
        NSLog(@"a = %d",a);
  
        
# pragma mark ** 直接对字符串进行大小写转换 API(case)
        
        NSString *str = @"abc";
        /* 字符全变成大写 */upper
        NSString *strUP = [str uppercaseString];
        NSLog(@"strUP:%@",strUP);
        /* 小写*/lower
        NSString *strlow = strUP.lowercaseString;
        NSLog(@"strlow:%@", strlow);
        /*首字母大写*/capital
        NSString *newStr = str.capitalizedString;
        NSLog(@"newStr:%@",newStr);
        
        /* 需求  将姓名的首字母提取(sub)出来  并且转换为大写字母 */
        NSString *name = @"zhagsan";
        NSString *Subname = [name substringToIndex:1].uppercaseString;/*变成大写字母*/
        NSLog(@"Subname:%@", Subname);
#pragma mark ** 直接判断字符串是否以 某某某 开头 hasPrefix 或结尾 hasSuffix
        
        NSString *str = @"http://www.baidu.com";
        if ([str hasPrefix:@"http"]) {
            NSLog(@"字符以http开头");
        }
        if ([str hasSuffix:@".com"]) {
            NSLog(@"字符以.com结尾");
        }
      
        
#pragma mark 练习
    /*http://www.lanou3g.com/icon.png
        判断上述字符串中是否以“png”结尾，如果是就替换成“jpg”，如果不是，就拼接”.jpg”。*/
        NSString *str = @"http://www.lanou3g.com/icon.png";
        if ([str hasSuffix:@"png"]) {
            str = [str stringByReplacingOccurrencesOfString:@"png" withString:@"jpg"];
        }else{
            str = [str stringByAppendingString:@".jpg"];
        }
        
        NSLog(@"str:%@",str);
        

        
        

# pragma mark - 知识点2 可变字符串
        
        /* 1.是NSString的子类,继承了NSSTring所有属性和方法
         * 2.可变意味着自身内容和长度是动态的(可变化的)
         */
# pragma mark ** 创建对象相关 API
        
        
        NSMutableString *mStr = [[NSMutableString alloc] initWithString:@"I have an phone"];
        
        NSMutableString *mStr2 = [NSMutableString stringWithString:@"iOS"];
        
        NSMutableString *mStr3 = [NSMutableString stringWithFormat:@"%@",mStr];

        
# pragma mark ** 拼接字符串 stringWithString API
        
        NSMutableString *mStr = [NSMutableString stringWithString:@"iphone"];
        /* 需求: "iphone " 后面加上"6s" */
        [mStr appendString:@"6s"];
        NSLog(@"mStr:%@",mStr);


        
        
#pragma mark ** 删除一个范围内的字符 API deleteCharactersInRange
        NSMutableString *mStr = [NSMutableString stringWithString:@"http://www.baidu.com"];
        /*需求  删除http://*/
        [mStr deleteCharactersInRange:[mStr rangeOfString:@"http://"]];
        NSLog(@"mStr:%@",mStr);

        
#pragma mark ** 字符串中插入相应的字符API insertString
        NSMutableString *mStr = [NSMutableString stringWithString:@"http://www.baidu.com"];
        /*需求  在http后面加上s*/
        [mStr insertString:@"s" atIndex:4];
        NSLog(@"mStr:%@",mStr);

#pragma mark ** 替换字符串API
        
        NSMutableString *mStr = [NSMutableString stringWithString:@"i have an phone"];
//        NSMutableString *mStr1 = [mStr1 stringByReplacingOccurrencesOfString:@"XiaoMi" withString:@"phone"];
//        NSLog(@"*mStr1:%@",mStr1);
        [mStr replaceCharactersInRange:[mStr rangeOfString:@"phone"] withString:@"Xiaomi"];
        NSLog(@"mStr:%@",mStr);

#pragma mark ** 重置字符串API setString

        NSMutableString *mStr = [NSMutableString stringWithString:@"iOS"];
        [mStr setString:@"iPhone"];
        NSLog(@"mStr:%@",mStr);

  
# pragma mark - 知识点3 NSNumber 类
        /* Class: NSNumber
         * 此类的作用就是基本数据类型和OC对象进行相互转换
         
         * 详见APi
         */
        
        /* 创建对象 */
        NSNumber *num1 = [NSNumber numberWithInt:20];
        NSLog(@"num1:%@",num1);
        
        int a = 100;
        NSNumber *num2 = [NSNumber numberWithInt:a];
        NSLog(@"num2:%@",num2);
        /*字面量创建*/
        NSNumber *num3 = @20;
        int b = 200;
        NSNumber *num4 = @(b);
       
        /* 从NSNumber对象转换成基本数据类型 */
        
        int aa = num1.intValue;
        NSLog(@"aa:%d",aa);
        
        /* NSNumber对象之间比较*/
        [num1 compare:num2];
        NSLog(@"%ld",[num1 compare:num2]);
        
#endif
# pragma mark - 知识点4 NSBValue location(在...之后的字符) length(长度)
        
        /** Class: NSValue
         * 作用 将结构体和对象类型进行互转
         */
        
//        
        NSRange range = {3, 4};
        
        /* 创建对象 */
        NSValue *val1 = [NSValue valueWithRange:range];
        NSLog(@"%@",val1);
        /*再转换成结构体*/
        NSRange newRange = val1.rangeValue;
        NSLog(@"%ld,%ld", newRange.location,newRange.length);
        
      

        
        
    }
    return 0;
}
